package anasazi;

import repast.simphony.context.Context;
import repast.simphony.context.space.continuous.ContinuousSpaceFactory;
import repast.simphony.context.space.continuous.ContinuousSpaceFactoryFinder;
import repast.simphony.dataLoader.ContextBuilder;
import repast.simphony.space.continuous.ContinuousSpace;
import repast.simphony.space.continuous.RandomCartesianAdder;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;

public class artificialAnasazi implements ContextBuilder<Object> {

	@Override
	public Context build(Context<Object> context) {
		context.setId("anasazi");
		ContinuousSpaceFactory spaceFactory = ContinuousSpaceFactoryFinder . createContinuousSpaceFactory ( null );
		ContinuousSpace<Object> space = spaceFactory . createContinuousSpace ("space", context ,
				new RandomCartesianAdder <Object>() , 
				new repast.simphony.space.continuous.WrapAroundBorders(),
				119, 45);
		ArrayList<Integer> mapArray = fileReader("C:/RepastSimphony-2.0-beta/map.txt");
		addMapToSpace(mapArray,context,space);
		ArrayList<Integer> settlementsArray = fileReader("C:/RepastSimphony-2.0-beta/settlements.txt");
		addSettlementsToSpace(settlementsArray,context,space);
		ArrayList<Integer> waterArray = fileReader("C:/RepastSimphony-2.0-beta/settlements.txt");
		addWaterToSpace(waterArray,context,space);
		return context;
	}

	private ArrayList<Integer> fileReader(String filePath) {
		ArrayList<Integer> array = new ArrayList<Integer>();
		try {
			BufferedReader inFile = new BufferedReader(new FileReader(filePath));
			String values = inFile.readLine();
			StringTokenizer tokenizer = new StringTokenizer(values);
			while (tokenizer.hasMoreTokens()){
				array.add(Integer.parseInt(tokenizer.nextToken()));
			}
		}	catch(Exception e){
			System.out.println(e);
		}
		return array;
	}
	private void addSettlementsToSpace(ArrayList<Integer> array, Context<Object> context, ContinuousSpace<Object> space){
		Iterator<Integer> iterator = array.iterator();
		while (iterator.hasNext()){
			ArrayList<Integer> arrayList = new ArrayList<Integer>();
			for (int i = 0; i < 12; i++){
				arrayList.add(iterator.next());
			}
			int sarg = arrayList.get(0);
			int meterNorth = arrayList.get(1);
			int meterEast = arrayList.get(2);
			int startDate = arrayList.get(3);
			int endDate = arrayList.get(4);
			int medianDate = arrayList.get(5);
			int typeSet = arrayList.get(6);
			int sizeSet = arrayList.get(7);
			int description = arrayList.get(8);
			int roomCount = arrayList.get(9);
			int elevation = arrayList.get(10);
			int baselineHouseholds = arrayList.get(11);
		}
	}
	
	private void addWaterToSpace(ArrayList<Integer> array, Context<Object> context, ContinuousSpace<Object> space){
		int year = 800;
		Iterator<Integer> iterator = array.iterator();
		Boolean streamsExist = false;
		Boolean alluviumExist = false;
		ArrayList<Integer> coordinateXList = new ArrayList<Integer>();
		ArrayList<Integer> coordinateYList = new ArrayList<Integer>();

		if ((year >= 280 && year < 360)){
			streamsExist = true;
		}
		else if(year >= 800 && year < 930){
			streamsExist = true;
		}
		else if(year >= 1300 && year < 1450){
			streamsExist = true;
		}
		if (year >= 420 && year < 560){
			alluviumExist = true;
		}
		else if (year >= 630 && year < 680){
			alluviumExist = true;
		}
		else if (year >= 980 && year < 1120){
			alluviumExist = true;
		}
		else if (year >= 1180 && year < 1230){
			alluviumExist = true;
		}
		while (iterator.hasNext()){
			ArrayList<Integer> arrayList = new ArrayList<Integer>();
			Boolean waterSource = false;
			for (int i = 0; i < 6; i++){
				arrayList.add(iterator.next());
			}
			int sarg = arrayList.get(0);
			int meterNorth = arrayList.get(1);
			int meterEast = arrayList.get(2);
			int typeWater = arrayList.get(3);
			int startDate = arrayList.get(4);
			int endDate = arrayList.get(5);
			int x = (int) (24.5+((meterEast-2392)/93.5));
			int y = (int) (45 +37.6 + ((meterNorth - 7954) / 93.5));
			
			if (x == 72 && y == 114){
				waterSource = true;
			}
			else if (x == 69 && y == 112){
				waterSource = true;
			}
			else if (x == 68 && y == 111){
				waterSource = true;
			}
			else if (x == 67 && y == 110){
				waterSource = true;
			}
			else if (x == 66 && y == 109){
				waterSource = true;
			}
			else if (x == 65 && y == 108){
				waterSource = true;
			}
			else if (x == 65 && y == 107){
				waterSource = true;
			}
			
			if (typeWater==2){
				waterSource = true;
			}
			else if (typeWater==3 && year >= startDate && year <= endDate){
				waterSource = true;
			}
			
			if (waterSource == true){
				context.add(new water(space));
				coordinateXList.add(x);
				coordinateYList.add(y);
			}
			
		}
	}

	private void addEnvironmentToSpace(ArrayList<Integer> array, Context<Object> context, ContinuousSpace<Object> space){
		Iterator<Integer> iterator = array.iterator();
		ArrayList<Integer> arrayList = new ArrayList<Integer>();
		while (iterator.hasNext()){
			for (int i = 0; i < 15; i++){
				arrayList.add(iterator.next());
			}
		}
	}
	private void addMapToSpace(ArrayList<Integer> array, Context<Object> context, ContinuousSpace<Object> space){
		Iterator<Integer> iterator = array.iterator();
		int value;
		while (iterator.hasNext()){
			value = iterator.next();
			if (value==60){
				context.add(new mapEmpty(space,value));
			}
			if (value==50){
				context.add(new mapKinbiko(space,value));
			}
			if (value==40){
				context.add(new mapUplands(space,value));
			}
			if (value==30){
				context.add(new mapNatural(space,value));
			}
			if (value==25){
				context.add(new mapMidDunes(space,value));
			}
			if (value==20){
				context.add(new mapMid(space,value));
			}
			if (value==15){
				context.add(new mapNorthDunes(space,value));
			}
			if (value==10){
				context.add(new mapNorth(space,value));
			}
			if (value==0){
				context.add(new mapGeneral(space,value));
			}
		}
		
		int x = 0;
		int y = 119;
		for (Object obj: context){
			space.moveTo(obj, x,y);			
			if (y>0){
				y--;
			}
			else{
				y=199;
				x++;
			}
		}
	}
}